package com.example.hxpc.guidancemanagement.activity;

import android.app.Activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.action.BroadcastAction;
import com.example.hxpc.guidancemanagement.fragment.LoginFragment;
import com.example.hxpc.guidancemanagement.fragment.RegisterFragment;

/**
 * Created by tao on 2017/11/26.
 * 登录页面
 */

public class LoginActivity extends Activity implements View.OnClickListener{
    private LinearLayout linearRegister;
    private LinearLayout linearLogin;
    private TextView te_register;
    private TextView te_login;
    private RelativeLayout rela_register;
    private RelativeLayout rela_login;
    private RegisterFragment registerFragment;
    private LoginFragment loginFragment;
    private FragmentManager fragmentManager;
    private Receiver receiver;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_login);
        initView();
    }

    //设置默认显示的frament
    private void setDefaultFrag(){
        if (fragmentManager == null){
            fragmentManager = getFragmentManager();
        }
        FragmentTransaction tr = fragmentManager.beginTransaction();
        if(registerFragment == null){
            registerFragment = new RegisterFragment();
        }
        if (loginFragment == null){
            loginFragment = new LoginFragment();
        }
        tr.add(R.id.frament, registerFragment)
                .add(R.id.frament, loginFragment)
                .hide(registerFragment)
                .show(loginFragment)
                .commit();
    }

    //显示frament函数
    private void showFrament(Fragment show,Fragment hide){
        if (fragmentManager == null){
            fragmentManager = getFragmentManager();
        }
        FragmentTransaction tr = fragmentManager.beginTransaction();
        tr.hide(hide).show(show).commit();
    }


    //初始化控件
    public void initView(){
        linearRegister = (LinearLayout) findViewById(R.id.linearRegister);
        linearRegister.setOnClickListener(this);
        linearLogin = (LinearLayout) findViewById(R.id.linearLogin);
        linearLogin.setOnClickListener(this);
        te_register = (TextView) findViewById(R.id.text_register);
        te_login = (TextView) findViewById(R.id.text_login);
        rela_register = (RelativeLayout) findViewById(R.id.rela_register);
        rela_login = (RelativeLayout) findViewById(R.id.rela_login);
        setDefaultFrag();
        //广播初始化
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadcastAction.registerAction);
        intentFilter.addAction(BroadcastAction.loginAction);
        receiver = new Receiver();
        registerReceiver(receiver,intentFilter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.linearRegister:
                te_register.setTextColor(this.getResources().getColor(R.color.blue));
                rela_register.setBackgroundResource(R.color.blue);
                te_login.setTextColor(this.getResources().getColor(R.color.dark));
                rela_login.setBackgroundResource(R.color.gray);
                LoginActivity.this.sendBroadcast(new Intent(BroadcastAction.registerAction));
                break;
            case R.id.linearLogin:
                te_login.setTextColor(this.getResources().getColor(R.color.blue));
                rela_login.setBackgroundResource(R.color.blue);
                te_register.setTextColor(this.getResources().getColor(R.color.dark));
                rela_register.setBackgroundResource(R.color.gray);
                LoginActivity.this.sendBroadcast(new Intent(BroadcastAction.loginAction));
                break;
        }
    }

    //给RegisterFragment回调用的方法
    public void showOnFragment(){
        showFrament(loginFragment, registerFragment);
        te_login.setTextColor(this.getResources().getColor(R.color.blue));
        rela_login.setBackgroundResource(R.color.blue);
        te_register.setTextColor(this.getResources().getColor(R.color.dark));
        rela_register.setBackgroundResource(R.color.gray);
    }

    public class Receiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals(BroadcastAction.registerAction)){
                showFrament(registerFragment, loginFragment);
            }else if (action.equals(BroadcastAction.loginAction)){
                showFrament(loginFragment, registerFragment);
            }

        }
    }
}
